package com.yxp.util;

import java.util.Random;

/**
 * @Auther : yxpweb
 * @Date : 2021/8/14 9:24
 * @Description: 初始化listNode 的方法
 */
public class ListNode {

    public int val;
    public ListNode next;

    public ListNode(int x) {
        val = x;
    }

    public static ListNode initListNode() {
        Random random = new Random(10);
        int i = 0;
        ListNode head = new ListNode(random.nextInt(10));
        ListNode tempNode = head;
        while (i < 5) {
            ListNode listNode = new ListNode(random.nextInt(10));
            tempNode.next = listNode;
            tempNode = listNode;
            i++;
        }
        return head;
    }

    public static ListNode initListNode(int[] numbers) {
        ListNode head = new ListNode(numbers[0]);
        ListNode tempNode = head;
        for (int i = 1; i < numbers.length; i++) {
            ListNode listNode = new ListNode(numbers[i]);
            tempNode.next = listNode;
            tempNode = listNode;
        }
        return head;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(val).append(";");
        ListNode tempNode = next;
        while (tempNode != null) {
            stringBuilder.append(tempNode.val).append(";");
            tempNode = tempNode.next;
        }
        return stringBuilder.toString();
    }
}
